//
//  Config.m
//  Cunli
//
//  Created by lilubin on 15/6/9.
//  Copyright (c) 2015年 lilubin. All rights reserved.
//

#import "Config.h"
#import "BaseUser.h"
#import "DetailUser.h"
#import "User.h"

NSString * const kUsername      = @"username";
NSString * const kUserID        = @"userid";
NSString * const kUserSession   = @"session";
NSString * const kUserAreaids   = @"areaids";
NSString * const kIsAutoLogin   = @"autoLogin";
NSString * const kMobileNum     = @"mobileNum";
NSString * const kPortraitURL   = @"portraitURL";
NSString * const kUserTruename  = @"userTruename";
NSString * const kUserPwd       = @"userPwd";
NSString * const kVillageName   = @"areaname";
NSString * const kLogintimes    = @"logintimes";
NSString * const kInviteDic     = @"invit";
NSString * const kUserInfo      = @"userinfo";
NSString * const kGroupID       = @"groupid";

@implementation Config

+ (NSString *)getOwnUsername
{
    /*
    if ([Login curLoginBaseUser] != nil) {
        return [Login curLoginBaseUser].username;
    }
    */
    NSUserDefaults *userDefault = [NSUserDefaults standardUserDefaults];
    NSString *username = [userDefault objectForKey:kUsername];
    if (username) {
        return username;
    }
    
    return nil;
    
}

+ (void)saveUserName:(NSString *)userName
{
    
    NSUserDefaults *userDefault = [NSUserDefaults standardUserDefaults];
    [userDefault setObject:userName forKey:kUsername];
    [userDefault synchronize];
    
}

+ (NSString *)getOwnUserID
{
    /*
    if ([Login curLoginBaseUser] != nil) {
        return [Login curLoginBaseUser].userid;
    }
    */
    NSString *userID = [[NSUserDefaults standardUserDefaults] objectForKey:kUserID];
    if (userID) {
        return userID;
    }
    return nil;
}

+ (void)saveUserID:(NSString *)userID
{
    
    NSUserDefaults *userDefault = [NSUserDefaults standardUserDefaults];
    [userDefault setObject:userID forKey:kUserID];
    [userDefault synchronize];
    
//    [Login curLoginBaseUser].userid = userID;
}

+ (NSString *)getOwnSession
{
    
    if ([Login curLoginBaseUser] != nil) {
        return [Login curLoginBaseUser].usersession;
    }
     
    NSUserDefaults *userDefault = [NSUserDefaults standardUserDefaults];
    NSString *usersession = [userDefault objectForKey:kUserSession];
    if (usersession) {
        return usersession;
    }
    return nil;
    
}

+ (void)saveSession:(NSString *)session
{
    
    NSUserDefaults *userDefault = [NSUserDefaults standardUserDefaults];
    [userDefault setObject:session forKey:kUserSession];
    [userDefault synchronize];
    
//    [Login curLoginBaseUser].usersession = session;
}

+ (NSString *)getOwnAreaids
{
    /*
    NSUserDefaults *userDefault = [NSUserDefaults standardUserDefaults];
    NSString *userAreaids = [userDefault objectForKey:kUserAreaids];
    if (userAreaids) {
        return userAreaids;
    }
    return nil;
     */
    return [Login curLoginBaseUser].areaids;
}

+ (void)saveAreaids:(NSString *)areaids
{
    /*
    NSUserDefaults *userDefault = [NSUserDefaults standardUserDefaults];
    [userDefault setObject:areaids forKey:kUserAreaids];
    [userDefault synchronize];
     */
    [Login curLoginBaseUser].areaids = areaids;
}

//设置自动登录的按钮
+ (BOOL)isAutoLogin
{
    NSUserDefaults *userDefault = [NSUserDefaults standardUserDefaults];
    BOOL isAutoLogin = [[userDefault objectForKey:kIsAutoLogin] boolValue];
    if (isAutoLogin) {
        return isAutoLogin;
    }
    
    return isAutoLogin;
}

+ (void)removeAutoLogin
{
    NSUserDefaults *userDefault = [NSUserDefaults standardUserDefaults];
    [userDefault removeObjectForKey:kIsAutoLogin];
}

+ (void)saveAutoLogin:(BOOL)isAutoLogin
{
    NSUserDefaults *userDefault = [NSUserDefaults standardUserDefaults];
    [userDefault setObject:[NSNumber numberWithBool:isAutoLogin] forKey:kIsAutoLogin];
    [userDefault synchronize];
}

+ (NSString *)getPortraitURL
{
    /*
    NSUserDefaults *userDefault = [NSUserDefaults standardUserDefaults];
    NSString *portraitStr = [userDefault objectForKey:kPortraitURL];
    if (portraitStr) {
        return portraitStr;
    }
    return nil;
     */
    return [Login curLoginDetailUser].pic;
}

+ (void)saveUserPortraitURL:(NSString *)portraitStr
{
    /*
    NSUserDefaults *userDefault = [NSUserDefaults standardUserDefaults];
    [userDefault setObject:portraitStr forKey:kPortraitURL];
    [userDefault synchronize];
     */
    [Login curLoginDetailUser].pic = portraitStr;
}

+ (NSString *)getUserTruename
{
    /*
    NSUserDefaults *userDefault = [NSUserDefaults standardUserDefaults];
    NSString *truenameStr = [userDefault objectForKey:kUserTruename];
    if (truenameStr) {
        return truenameStr;
    }
    return nil;
     */
    return [Login curLoginBaseUser].truename;
}

+ (void)saveUserTruename:(NSString *)truename
{
    /*
    NSUserDefaults *userDefault = [NSUserDefaults standardUserDefaults];
    [userDefault setObject:truename forKey:kUserTruename];
    [userDefault synchronize];
     */
    [Login curLoginBaseUser].truename = truename;
}

+ (NSString *)getUserPwd
{
    NSUserDefaults *userDefault = [NSUserDefaults standardUserDefaults];
    NSString *pwdStr = [userDefault objectForKey:kUserPwd];
    if (pwdStr) {
        return pwdStr;
    }
    return nil;
}

+(void)saveUserPwd:(NSString *)userPwd
{
    NSUserDefaults *userDefault = [NSUserDefaults standardUserDefaults];
    [userDefault setObject:userPwd forKey:kUserPwd];
    [userDefault synchronize];
}

+ (NSString *)getVillageName
{
    /*
    NSUserDefaults *userDefault = [NSUserDefaults standardUserDefaults];
    NSString *village = [userDefault objectForKey:kVillageName];
    if (village) {
        return village;
    }
    return @"";
     */
    return [Login curLoginBaseUser].areaname;
}

+ (void)saveVillageName:(NSString *)village
{
    /*
    NSUserDefaults *userDefault = [NSUserDefaults standardUserDefaults];
    [userDefault setObject:village forKey:kVillageName];
    [userDefault synchronize];
     */
    [Login curLoginBaseUser].areaname = village;
}

+ (NSString *)getLogintimes
{
    
    NSUserDefaults *userDefault = [NSUserDefaults standardUserDefaults];
    NSString *kTimes = [userDefault objectForKey:kLogintimes];
    if (kTimes) {
        return kTimes;
    }
    return @"";
     
//    return [Login curLoginBaseUser].logintimes;
}

+ (void)saveLogintimes:(NSString *)times
{
    
    NSUserDefaults *userDefault = [NSUserDefaults standardUserDefaults];
    [userDefault setObject:times forKey:kLogintimes];
    [userDefault synchronize];
     
//    [Login curLoginBaseUser].logintimes = times;
}

+ (NSString *)getGroupID
{
    /*
    NSUserDefaults *userDefault = [NSUserDefaults standardUserDefaults];
    NSString *groupID = [userDefault objectForKey:kGroupID];
    if (groupID) {
        return groupID;
    }
    return @"";
     */
    return [Login curLoginBaseUser].groupid;
}

+ (void)saveGroupID:(NSString *)groupID
{
    /*
    NSUserDefaults *userDefault = [NSUserDefaults standardUserDefaults];
    [userDefault setObject:groupID forKey:kGroupID];
    [userDefault synchronize];
     */
    [Login curLoginBaseUser].groupid = groupID;
}

+(NSDictionary *)getInviteDic
{
    /*
    NSUserDefaults *userDefault = [NSUserDefaults standardUserDefaults];
    NSDictionary *inviteDic = [userDefault objectForKey:kInviteDic];
    if (inviteDic) {
        return inviteDic;
    }
    return nil;
     */
    return [Login curLoginBaseUser].invit;
}

+(void)saveInviteDic:(NSDictionary *)inviteDic
{
    /*
    NSUserDefaults *userDefault = [NSUserDefaults standardUserDefaults];
    [userDefault setObject:inviteDic forKey:kInviteDic];
    [userDefault synchronize];
     */
    [Login curLoginBaseUser].invit = inviteDic;
}

+(NSDictionary *)getUserInfoDic
{
    NSUserDefaults *userDefault = [NSUserDefaults standardUserDefaults];
    NSDictionary *userInfoDic = [userDefault objectForKey:kUserInfo];
    if (userInfoDic) {
        return userInfoDic;
    }
    return nil;
}

+(void)saveUserInfoDic:(NSDictionary *)userInfo
{
    NSUserDefaults *userDefault = [NSUserDefaults standardUserDefaults];
    [userDefault setObject:userInfo forKey:kUserInfo];
    [userDefault synchronize];
}

+ (void)removeLogin
{
    NSUserDefaults *userDefault = [NSUserDefaults standardUserDefaults];
    [userDefault removeObjectForKey:kUsername];
    [userDefault removeObjectForKey:kUserSession];
    [userDefault removeObjectForKey:kUserID];
    /*
    [userDefault removeObjectForKey:kUsername];
    [userDefault removeObjectForKey:kUserPwd];
    [userDefault removeObjectForKey:kUserAreaids];
    [userDefault removeObjectForKey:kUserTruename];
    [userDefault removeObjectForKey:kPortraitURL];
    [userDefault removeObjectForKey:kLogintimes];
    [userDefault removeObjectForKey:kVillageName];
     */
    //    [userDefault removeObjectForKey:kInviteDic];
    [Login doLogout];
    
}


+ (MBProgressHUD *)createHUD
{
    UIWindow *window = [[UIApplication sharedApplication].windows lastObject];
    MBProgressHUD *HUD = [[MBProgressHUD alloc] initWithWindow:window];
    HUD.detailsLabelFont = [UIFont boldSystemFontOfSize:16];
    [window addSubview:HUD];
    [HUD show:YES];
    return HUD;
}

+ (void)showHUDWithText:(NSString *)str andImage:(NSString *)imgStr
{
    MBProgressHUD *hud = [Config createHUD];
    hud.mode = MBProgressHUDModeCustomView;
    hud.customView = [[UIImageView alloc] initWithImage:[UIImage imageNamed:imgStr]];
    hud.labelText = str;
    [hud hide:YES afterDelay:1];
}

+(BOOL)isShield{
    
//    NSDictionary *user=[[NSUserDefaults standardUserDefaults]objectForKey:@"userinfo"];
//    NSString *userRoleString=[user objectForKey:@"is_banned"];
//    return [userRoleString isEqualToString:@"1"]? YES : NO ;
    return [[Login curLoginBaseUser].is_banned isEqualToString:@"1"]? YES : NO;
    
    /*
    if ([userRoleString isEqualToString:@"1"]) {
        return YES;
    }else{
        return NO;
    }
     */
}

@end
